/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.GestionEspacioDeportivo;

import Controlador.ControladorEspacioDeportivo;
import Logica.EspacioDeportivo;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Invitado
 */
public class PanelEliminarEspacioDeportivo extends javax.swing.JPanel {

    /**
     * Creates new form PanelEditarEspacioDeportivo
     */
    ArrayList<String> idsEspacioDeportivos;
    ControladorEspacioDeportivo controladorEspacio;
    
    public PanelEliminarEspacioDeportivo() {
        initComponents();
        this.controladorEspacio = new ControladorEspacioDeportivo();
        this.cargarEspaciosDeportivo();
        this.jPanelDatos.setVisible(false);
    }
    
    private void cargarEspaciosDeportivo()
    {
        this.jCBEspaciosDeportivo.removeAllItems();
        Vector<EspacioDeportivo> espacios = (Vector<EspacioDeportivo>) this.controladorEspacio.consultarEspaciosDeportivos();
        
        for(int i=0; i<espacios.size(); i++)
        {
            EspacioDeportivo espacio = espacios.get(i);
            String datosEspacio = "";
            this.idsEspacioDeportivos.add(espacio.getIdEspacio());
            
            datosEspacio+=espacio.getNombre()+" - "+espacio.getDireccion();
            this.jCBEspaciosDeportivo.addItem(datosEspacio);
        }
    }
    
    private void cargarDatosEspacioDeportivo(EspacioDeportivo espacio)
    {
        this.jTFNombre.setText(espacio.getNombre());
        this.jTFDireccion.setText(espacio.getNombre());
        this.jTFCapacidad.setText(""+espacio.getCapacidad());
    }
    
    private void limpiarDatos()
    {
        this.jTFNombre.setText("");
        this.jTFDireccion.setText("");
        this.jTFCapacidad.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jCBEspaciosDeportivo = new javax.swing.JComboBox();
        jPanelDatos = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTFNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTFCapacidad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTFDireccion = new javax.swing.JTextField();
        jBEditar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();

        jLabel1.setText("Espacio Deportivo");

        jCBEspaciosDeportivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBEspaciosDeportivoActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre");

        jLabel3.setText("Capacidad");

        jLabel4.setText("Dirección");

        jBEditar.setText("Elminar");
        jBEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEditarActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelDatosLayout = new javax.swing.GroupLayout(jPanelDatos);
        jPanelDatos.setLayout(jPanelDatosLayout);
        jPanelDatosLayout.setHorizontalGroup(
            jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosLayout.createSequentialGroup()
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDatosLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanelDatosLayout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jTFDireccion))
                            .addGroup(jPanelDatosLayout.createSequentialGroup()
                                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTFCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanelDatosLayout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(jBEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBCancelar)))
                .addContainerGap(94, Short.MAX_VALUE))
        );
        jPanelDatosLayout.setVerticalGroup(
            jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTFCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTFDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBEditar)
                    .addComponent(jBCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBEspaciosDeportivo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCBEspaciosDeportivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEditarActionPerformed
        // TODO add your handling code here:
        try
        {
            int indice = this.jCBEspaciosDeportivo.getSelectedIndex();
            String idEspacio = this.idsEspacioDeportivos.get(indice);
            
            boolean exito;
            
            exito = this.controladorEspacio.eliminarEspacioDeportivo(idEspacio);
            if(exito)
            {
                JOptionPane.showMessageDialog(this, "La Operación se Realizo con Exito", "Exito", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        catch(NumberFormatException | HeadlessException e)
        {
            JOptionPane.showMessageDialog(this, "El valor de capacidad debe ser un numero entero, no negativo", "Error de tipo", JOptionPane.ERROR_MESSAGE);
        }
        this.cargarEspaciosDeportivo();
    }//GEN-LAST:event_jBEditarActionPerformed

    private void jCBEspaciosDeportivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBEspaciosDeportivoActionPerformed
        // TODO add your handling code here:
        int indice = this.jCBEspaciosDeportivo.getSelectedIndex();
        String idEspacio = this.idsEspacioDeportivos.get(indice);
        EspacioDeportivo espacio = this.controladorEspacio.consultarEspacioDeportivo(idEspacio);
        this.cargarDatosEspacioDeportivo(espacio);
        this.jPanelDatos.setVisible(true);
        this.jCBEspaciosDeportivo.setEnabled(false);
        
    }//GEN-LAST:event_jCBEspaciosDeportivoActionPerformed

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        // TODO add your handling code here:
        this.limpiarDatos();
        this.jPanelDatos.setVisible(false);
        this.jCBEspaciosDeportivo.setEnabled(true);
    }//GEN-LAST:event_jBCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBEditar;
    private javax.swing.JComboBox jCBEspaciosDeportivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanelDatos;
    private javax.swing.JTextField jTFCapacidad;
    private javax.swing.JTextField jTFDireccion;
    private javax.swing.JTextField jTFNombre;
    // End of variables declaration//GEN-END:variables
}
